package server.main.utils.mainutils;


import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.json.JSONException;
import org.json.JSONObject;
import server.main.pojo.ConfigBean;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class JsonUtils {

    /**
     * 一次性读取全部文件数据
     * @param filename
     */
    public static String readFile(String filename){
        try{
            InputStream is = new FileInputStream(filename);
            int iAvail = is.available();
            byte[] bytes = new byte[iAvail];
            is.read(bytes);
            String str = new String(bytes);
//            System.out.println(str);
//            logger.info("文件内容:\n" + new String(bytes));
            is.close();
            return str;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取txt文本中字段的信息
     * @param path 文本路径
     * @return
     */
    public static Map<String, String> GetTxtparam(String path) {
        Map<String, String> map = new HashMap<>();
        String strtemp = "";
        try {
            String lines;
            File file = new File(path);
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            while ((lines = bufferedReader.readLine()) != null) {
//                System.out.println(lines);
                String[] split = lines.split("=");
                map.put(split[0], split[1]);
            }


        } catch (Exception e) {
            e.printStackTrace();

        }

//        List<String> goal = new ArrayList<>();

//        String mainfilepath = readFile(path);
//        JSONObject myJson = null;
//        try {
//            myJson = new JSONObject(mainfilepath);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return  myJson.optString(param);
        return map;
    }
    /**
     * config.json文件的解析
     * @param filename
     * @return
     */
   public static ConfigBean GetConfigBean(String filename){
       String JsonStr = readFile(filename);
       ConfigBean configBean = new ConfigBean();
       Gson gson = new Gson();
       try {
           configBean = gson.fromJson(JsonStr, ConfigBean.class);
           return configBean;
       } catch (JsonSyntaxException e) {
           e.printStackTrace();
       }
       return null;
   }

    /**
     * 获取工程的绝对路径
     * @param param
     * @return
     */
    public static String Getjsonparam(String param){
        String mainfilepath = readFile("config.json");
        JSONObject myJson = null;
        try {
            myJson = new JSONObject(mainfilepath);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return  myJson.optString(param);
    }
    public static String getmainpath(){
        try{
            InputStream is = JsonUtils.class.getClassLoader().getResourceAsStream("config.json");
            int iAvail = is.available();
            byte[] bytes = new byte[iAvail];
            is.read(bytes);
            String str = new String(bytes);
//            System.out.println(str);
//            logger.info("文件内容:\n" + new String(bytes));
            is.close();
            JSONObject myJson = new JSONObject(str);
            return  myJson.optString("project_path");
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;

    }

//    public static void main(String[] args) {
//        ConfigBean configBean = GetConfigBean("C:\\Users\\MingHs\\Desktop\\1\\src\\main\\java\\com\\main\\utils\\config.json");
//        System.out.println(configBean);
//    }

}
